﻿namespace Beetle
{
    using System;
    using System.Collections.Generic;

    public class ByteArrayPool
    {
        private Stack<ByteArraySegment> _field_a = new Stack<ByteArraySegment>();
        private int _field_b;

        public ByteArrayPool(int initcount, int length)
        {
            this._field_b = length;
            for (int i = 0; i < initcount; i++)
            {
                this._field_a.Push(this.a());
            }
        }

        private ByteArraySegment a()
        {
            return new ByteArraySegment(this._field_b);
        }

        public ByteArraySegment Pop()
        {
            lock (this._field_a)
            {
                if (this._field_a.Count > 0)
                {
                    return this._field_a.Pop();
                }
                return this.a();
            }
        }

        public void Push(ByteArraySegment item)
        {
            lock (this._field_a)
            {
                this._field_a.Push(item);
            }
        }

        public int Count
        {
            get
            {
                return this._field_a.Count;
            }
        }
    }
}

